﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OL.FrameCore.Application.CommonData;
using OL.FrameCore.Application.Dept;
using OL.FrameCore.Application.Dept.Dto;
using OL.FrameCore.WebUI.ViewModels;
using OL.FrameCore.WebUI.ViewModels.Dept;

namespace OL.FrameCore.WebUI.Controllers
{
    public class DeptController : ControllerBase
    {
        IDeptService _deptService;
        ICommonDataService _commonDataService;

        public DeptController(IDeptService deptService, ICommonDataService commonDataService)
        {
            _deptService = deptService;
            _commonDataService = commonDataService;
        }

        public IActionResult Index()
        {
            ViewBag.DeptList = _commonDataService.GetDeptDropDownList();
            return View();
        }

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult GetDeptList(GetDetpListViewRequest request)
        {
            var serviceRequest = ToMap<GetDetpListViewRequest, GetDeptListRequest>(request);
            serviceRequest.PageIndex = request.page - 1;
            serviceRequest.PageSize = request.limit;

            var serviceResult = _deptService.GetDeptList(serviceRequest);
            var result = new
            {
                code = 0,
                msg = "",
                count = serviceResult.ItemsCount,
                data = serviceResult.Items
            };

            return Json(result);
        }

        public IActionResult GetDeptTree()
        {
            var serviceResult = _deptService.GetAllDeptList();
            var result = CreateDeptTreeNode(serviceResult);
            return Json(result);
        }
        private List<TreeNodeView> CreateDeptTreeNode(IList<GetDeptListModel> list, int parentId = 0)
        {
            List<TreeNodeView> result = new List<TreeNodeView>();

            var listTmp = list.Where(p => p.ParentId == parentId).ToList();
            foreach (var li in listTmp)
            {
                TreeNodeView model = new TreeNodeView
                {
                    id = li.Id,
                    title = li.DeptName,
                    field = "",
                    children = CreateDeptTreeNode(list, li.Id)
                };
                result.Add(model);
            }

            return result;
        }

        /// <summary>
        /// 添加或修改部门页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult AddOrUpdateDept(int? id)
        {
            ViewBag.DeptList = _commonDataService.GetDeptDropDownList();

            GetDeptResponse model = new GetDeptResponse();
            if (id != null)
            {
                model = _deptService.GetDept(id.Value);
            }
            return View(model);
        }

        /// <summary>
        /// 添加或修改部门
        /// </summary>
        /// <param name="requet"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostAddOrUpdateDept(PostAddOrUpdateDeptViewRequest request)
        {
            //验证模型数据
            if (IsNotValidModel)
            {
                return FailResult(ModelErrorMessage);
            }

            if (request.Id == null || request.Id == 0)//添加
            {
                var serviceRequest = ToMap<PostAddOrUpdateDeptViewRequest, AddDeptRequest>(request);
                serviceRequest.OpeatorId = UserId;
                var result = _deptService.AddDept(serviceRequest);
                return Json(result);
            }
            else//修改
            {
                var serviceRequest = ToMap<PostAddOrUpdateDeptViewRequest, UpdateDeptRequest>(request);
                serviceRequest.OpeatorId = UserId;
                var result = _deptService.UpdateDept(serviceRequest);
                return Json(result);
            }
            
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteDept(int id)
        {
            var result = _deptService.DeleteDept(id, UserId);
            return Json(result);
        }
    }
}